﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Serilog.Core;
using System.Net;
using System.Net.Sockets;

namespace T_Material.API
{
    public class ExceptionFilter: IExceptionFilter
    {
        private readonly Logger<ExceptionFilter> logger1;
        public ExceptionFilter(Logger<ExceptionFilter> logger1)
        {
            this.logger1 = logger1;
        }

        public void OnException(ExceptionContext context)
        {
            var hostName=Dns.GetHostName();
            var hostEntry=Dns.GetHostEntry(hostName);
            var ipAddress = hostEntry.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);

            logger1.LogError(ipAddress + context.Exception.StackTrace[..50] + "康建东发生了异常");
            context.ExceptionHandled = true;
        }
    }
}
